import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCselect {

    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        Connection connection = dataSource.getConnection();

        //构建sql语句
        String sql = "select * from student";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);


        //执行sql语句  打印临时表
        ResultSet resultSet = preparedStatement.executeQuery();
        //ResultSet是一个类似于迭代器一样的东西
        //Next每次读取一行数据  没到达末尾返回true 到达末尾返回false 结束循环

        while(resultSet.next()) {
            int a = resultSet.getInt("id");
            String b = resultSet.getString("name");

            System.out.println("id:" + a + " " + "name:" + b);
        }

        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
